home *** CD-ROM | disk | FTP | other *** search
- Path: news.unt.edu!news
- From: Steve Fogoros <sfogoros@hsc.unt.edu>
- Newsgroups: comp.lang.c
- Subject: Re: HELP!
- Date: Sun, 10 Mar 1996 03:37:44 -0800
- Organization: University of North Texas Health Science Center
- Message-ID: <3142BF08.1F5C@hsc.unt.edu>
- References: <4htsjm$v5o@lantana.singnet.com.sg>
- NNTP-Posting-Host: sfogoros.hsc.unt.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Teddy Bear wrote:
- >
- > Can anyone please help me my giving me a sample programme?
- > I need to write a program that will enter and record an individual's
- > details
- > search (by name) and display an individual's details
- > Delete an individual's details
- > Save and restore the entire list to/from a disk file
- >
- > Emergency!
- > Thanks
- > my e-mail add is at s7700038@singnet.com.sg
-
- Here is a most basic sample program:
-
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
-
- void enter(void);
- void show(void);
- void delete(void);
-
- FILE *fi, *fo;
-
- void main(void)
- {
- printf("1. enter a record\n");
- printf("2. show a record\n");
- printf("3. delete a record\n");
- printf("4. exit\n");
- printf("Press 1, 2, 3, or 4 then enter\n");
-
- switch((char)getc(stdin))
- {
- case '1': getc(stdin); enter(); break;
- case '2': getc(stdin); show(); break;
- case '3': getc(stdin); delete(); break;
- case '4': exit(0); break;
- default: printf("bad selection. program terminated\n"); exit(0); break;
- }
- }
-
- void enter(void)
- {
- char buff[80];
-
- printf("Enter data followed by return key\n");
- fgets(buff,79,stdin);
-
- fi = fopen("dfile","a");
- if(fi == 0)
- {
- printf("unable to open dfile. program terminated\n");
- exit(0);
- }
-
- fprintf(fi,"%s",buff);
-
- fclose(fi);
- }
-
- void show(void)
- {
- char srch[80], buff[80];
-
- printf("Enter search string and enter\n");
- fgets(srch,79,stdin);
- srch[strlen(srch)-1] = 0;
- printf("\n");
-
- fi = fopen("dfile","r");
- if(fi == 0)
- {
- printf("unable to open dfile. program terminated\n");
- exit(0);
- }
-
- while(!feof(fi))
- {
- fgets(buff,79,fi);
- if(feof(fi)) break;
- if(strstr(buff,srch)) printf("%s",buff);
- }
-
- fclose(fi);
- }
-
- void delete(void)
- {
- char srch[80], buff[80];
-
- printf("Enter search string for delete then enter\n");
- fgets(srch,79,stdin);
- srch[strlen(srch)-1] = 0;
-
- fi = fopen("dfile","r");
- if(fi == 0)
- {
- printf("unable to open dfile. program terminated\n");
- exit(0);
- }
-
- fo = fopen("dfilf","w");
- if(fo == 0)
- {
- printf("unable to open dfilf. program terminated\n");
- exit(0);
- }
-
- while(!feof(fi))
- {
- fgets(buff,79,fi);
- if(feof(fi)) break;
- if(strstr(buff,srch))
- {
- printf("delete this record? press y then enter if yes\n%s",buff);
- if((char)getc(stdin) == 'y')
- continue;
- }
- fprintf(fo,"%s",buff);
- }
-
- fclose(fo);
- fclose(fi);
-
- fi = fopen("dfilf","r");
- if(fi == 0)
- {
- printf("unable to open dfilf. program terminated\n");
- exit(0);
- }
-
- fo = fopen("dfile","w");
- if(fo == 0)
- {
- printf("unable to open dfile. program terminated\n");
- exit(0);
- }
-
- while(!feof(fi))
- {
- fgets(buff,79,fi);
- if(feof(fi)) break;
- fprintf(fo,"%s",buff);
- }
-
- fclose(fo);
- fclose(fi);
- }
-
- --
- Steve Fogoros, Academic Information Coordinator
- University of North Texas Health Science Center
- sfogoros@hsc.unt.edu
-